package com.zhong.gang.app.ui.login;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import androidx.annotation.NonNull;
import com.zhong.gang.app.databinding.ActivityLoginBinding;
import com.zhong.gang.app.R;
import com.zhong.gang.applib.base.act.ActBase;
import com.zhong.gang.applib.utils.CheckedUtils;

public class LoginAct  extends ActBase<ActivityLoginBinding> {

    private LoginPresenter loginPresenter;

    @Override
    public int LayoutRes() {
        return R.layout.activity_login;
    }

    @NonNull
    @Override
    public String CreateTitle() {
        return "登录";
    }

    @Override
    public void initView() {
        getTitleBar().setVisibility(View.GONE);
        bind.setAct(this);
        bind.phone.addTextChangedListener(new mTextWatcher(bind.errorPhone));
        bind.pass.addTextChangedListener(new mTextWatcher(bind.errpass));
        loginPresenter = new LoginPresenter();
    }

    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.login:
                if (checkPass()){
                    loginPresenter.login(bind.phone.getText().toString(),bind.pass.getText().toString(),bind.errpass,this);
                }
                break;
            case R.id.forget_pass:
                openActivity(ForgetPassAct.class);
                break;
        }
    }

    /**
     * 判断是否输入正确
     */
    public boolean checkPass(){
        String pass = bind.pass.getText().toString();
        String phone = bind.phone.getText().toString();
        if (TextUtils.isEmpty(phone)){
            bind.errorPhone.setVisibility(View.VISIBLE);
            CheckedUtils.shakeAnimation(bind.phone,3);
            return false;
        }
        if (TextUtils.isEmpty(pass)){
            bind.errpass.setVisibility(View.VISIBLE);
            CheckedUtils.shakeAnimation(bind.pass,3);
            return false;
        }
        return true;
    }

    private class mTextWatcher implements TextWatcher {
        private View view;
        private mTextWatcher(View view){
            this.view = view;
        }
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            view.setVisibility(View.GONE);
        }

    }
}
